/****************************************************************************
 *  QLyrics - A simple lyrics viewer.                                       *
 *  Copyright (C) 2008  hsys <hanshuiys@gmail.com>                          *
 *                                                                          *
 *  This program is free software; you can redistribute it and/or modify    *
 *  it under the terms of the GNU General Public License as published by    *
 *  the Free Software Foundation; either version 3 of the License, or       *
 *  (at your option) any later version.                                     *
 *                                                                          *
 *  This program is distributed in the hope that it will be useful,         *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 *  GNU General Public License for more details.                            *
 *                                                                          *
 *  You should have received a copy of the GNU General Public License       *
 *  along with this program; if not, write to the Free Software Foundation, *
 *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA       *
 ****************************************************************************/

#ifndef _LYRICSPROVIDER_H_
#define _LYRICSPROVIDER_H_

#include <vector>
#include <QObject>
#include <QHttp>
#include <QFile>
#include "basictype.h"

/** This class is used to search lyrics. */
class LyricsProvider : public QObject {
    Q_OBJECT
public:
    /** Search result. */
    struct SearchResult {
        QString title, album, artist;
        QString preview;
        QString url;
    };
    /** Lyrics status. */
    enum LyricsStatus {
        /** Successfully download the lyrics. */
        Success,
        /** Searching the lyrics. */
        Searching,
        /** Downloading the lyrics. */
        Downloading,
        /** Lyrics not found. */
        NotFound,
        /** Network error. */
        NetworkError,
        /** I/O error when downloading lyrics. */
        IOError,
        /** Downloading process aborted. */
        Aborted
    };
    /** Class used to build specific provider. */
    class Builder {
        public:
            /** Create a new provider instance of current provider. */
            virtual LyricsProvider* create(const Track&, QObject* parent = 0) const = 0;
            /** Return provider name. */
            virtual const QString& getProviderName() const = 0;
    };

    /** The iterator type of search result. */
    typedef std::vector<SearchResult>::const_iterator const_iterator;

    LyricsProvider(QObject* parent = 0) : QObject(parent) {};

    /** Return the number of results. */
    int resultSize() const { return m_result.size(); };
    /** Return the iterator pointing to the beginning of the results. */
    const_iterator resultBegin() const { return m_result.begin(); };
    /** Return the iterator pointing to the end of the results. */
    const_iterator resultEnd() const {  return m_result.end(); };

    LyricsStatus getStatus() const { return m_status; };
    const Track& getTrack() const { return m_track; };

    /** Search the corresponding lyrics to the track. */
    virtual void search() = 0;
    /** Download the specific result. */
    virtual void acceptResult(int id);
    /** Cancel the result. */
    virtual void cancelResult();
    /** Return the filename of downloaded lyrics. */
    QString getFilename() const;

private:
    QHttp* m_http;
    QFile* m_file;
    int m_downloadId;

private slots:
    void slotDownloadDone(bool error);

protected:
    std::vector<SearchResult> m_result;
    Track m_track;
    LyricsStatus m_status;

    /** Return encoded form of url. */
    static QString encodeUrlQuery(const QByteArray& url);
signals:
    void signalSearched();
    void signalReady();
};

#endif
